﻿// ------------------------------------------------------------------------------------
//      Copyright (c) uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 2/26/2013 9:10:59 PM
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Service.Services
{
    using CP.NLayer.Models.Business;
    using CP.NLayer.Models.Entities;
    using CP.NLayer.Service.Contracts;
    using Microsoft.Practices.Unity;
    using System.Collections.Generic;
    using System.ServiceModel;

    [ErrorHandlingBehavior]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class RoleDisplayModelService : IRoleDisplayModelService
    {
        #region IDisplayModelService<RoleDisplayModel>
        public int GetCount()
        {
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            return service.GetCount();
        }

        public IList<RoleDisplayModel> GetPage(int pageIndex, int pageSize)
        {
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            var roles = service.GetPage(pageIndex, pageSize);
            return BuildModels(roles);
        }

        public void Delete(RoleDisplayModel obj)
        {
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            service.DeleteById(GetId(obj));
        }

        public string GetId(RoleDisplayModel obj)
        {
            return obj.Role.Id;
        }
        #endregion

        private RoleDisplayModel BuildModel(Role role)
        {
            if (role == null)
            {
                return null;
            }

            return new RoleDisplayModel()
            {
                Role = role
            };
        }

        private IList<RoleDisplayModel> BuildModels(IList<Role> roles)
        {
            if (roles == null)
            {
                return null;
            }
            var list = new List<RoleDisplayModel>();
            foreach (var obj in roles)
            {
                list.Add(BuildModel(obj));
            }
            return list;
        }
    }
}
